﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Transactions;
using Common.Background.Entity;
using Common.Background.Data;

namespace Common.Background.Service
{
    public partial class SystemServices : ServiceBase, IWebManageMenus
    {
        public List<WebManageMenus> WebManageMenusList(WebManageMenus WebManageMenus, VPredication vp)
        {
            return base.iWebManageMenusRepository.GetModel().OrderBy(item => item.SortNumber).ToList();
        }

        public WebManageMenus WebManageMenusTree()
        {
            WebManageMenus WebManageMenu = new WebManageMenus() { MenuID = 0 };
            WebManageMenu.SubWebManageMenus = WebManageMenusTree(WebManageMenu);
            return WebManageMenu;
        }

        public List<WebManageMenus> WebManageMenusTree(WebManageMenus WebManageMenus)
        {
            return (from _objWMMR in iWebManageMenusRepository.GetModel()
                    where _objWMMR.ParentID == WebManageMenus.MenuID
                    select new WebManageMenus_Ext
                   {
                       MenuID = _objWMMR.MenuID,
                       MenuName = _objWMMR.MenuName,
                       SortNumber = _objWMMR.SortNumber,
                       LinkUrl = _objWMMR.LinkUrl,
                       MenuLevel = _objWMMR.MenuLevel,
                       SubWebManageMenus = WebManageMenusTree(_objWMMR),
                       About = _objWMMR.About
                   }
            ).Cast<WebManageMenus>().OrderBy(i => i.SortNumber).ToList();
        }

        #region 得到菜单
        public WebManageMenus GetWebManageMenus(WebManageMenus WebManageMenus)
        {
            return base.iWebManageMenusRepository.Find(WebManageMenus.MenuID);
        }
        #endregion
        #region 设置菜单
        public void SetWebManageMenus(WebManageMenus WebManageMenus)
        {
            WebManageMenus.UpdateDate = DateTime.Now;
            base.iWebManageMenusRepository.Update(WebManageMenus);
        }
        #endregion
        #region 添加列表
        public void AddWebManageMenus(WebManageMenus WebManageMenus)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    WebManageMenus.Operator = string.Empty;
                    WebManageMenus.MenuID = new PKIDTableRepository().GetNewPKID(PKIDTable.WebManageMenus);
                    if (WebManageMenus.ParentID == 0)
                        WebManageMenus.MenuLevel = 1;
                    else
                    {
                        WebManageMenus.MenuLevel = base.iWebManageMenusRepository.GetModel().Where(i => i.MenuID == WebManageMenus.ParentID).First().MenuLevel + 1;
                    }
                    WebManageMenus.UpdateDate = DateTime.Now;
                    iWebManageMenusRepository.Insert(WebManageMenus);
                    trans.Complete();
                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {
                    trans.Dispose();
                }
            }



        }
        #endregion
        #region  删除菜单
        public void DeleteManageMenus(WebManageMenus WebManageMenus)
        {
            base.iWebManageMenusRepository.Delete(iWebManageMenusRepository.Find(WebManageMenus.MenuID));
        }
        #endregion
    }
}
